package cn.yunhe.servlet;

import cn.yunhe.dao.IAdminDao;
import cn.yunhe.dao.impl.AdminDao;
import cn.yunhe.service.RegService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class RegServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String rePassword = request.getParameter("rePassword");
        RegService regService = new RegService();

        if (!password.equals(rePassword)) {//判断重复密码是否一致
            out.print("<script>alert('两次密码输入不一致,请重新输入!!!');location.href='reg.html'</script>");
        } else if (regService.addAdmin(username, password)) {
            out.print("<script>alert('注册成功!!!');location.href='login.html'</script>");
        } else
            out.print("<script>alert('注册失败,请联系管理员!!!')</script>");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
